<style>
	.ui-autocomplete-loading { background: white url('/img/ui-anim_basic_16x16.gif') right center no-repeat; }
	.ui-autocomplete {
		max-height: 200px;
		overflow-y: auto;
		/* prevent horizontal scrollbar */
		overflow-x: hidden;
		/* add padding to account for vertical scrollbar */
		padding-right: 20px;
	}
	/* IE 6 doesn't support max-height
	 * we use height instead, but this forces the menu to always be this tall
	 */
	* html .ui-autocomplete {
		height: 100px;
	}
</style>
<?php 
echo $this->Html->css('jquery.ui.autocomplete');


echo $this->Html->script('jquery.ui.position');
echo $this->Html->script('jquery.ui.autocomplete');
?>
<script type="text/javascript">
<!--


$(function() {
	$(".soluong").keydown(function(e) {
		arr_key = new Array();
		arr_key[8]=8;
		arr_key[9]=9;
		arr_key[35]=35;
		arr_key[36]=36;
		arr_key[37]=37;
		arr_key[39]=39;
		arr_key[46]=46;
        if ( arr_key[e.keyCode]!==undefined) {}
        else {
            if (e.keyCode < 48 || e.keyCode > 57 ) {
                e.preventDefault(); 
            }   
        }
    });
	$( ".thoigian" ).datepicker({
								showOtherMonths: true,
								selectOtherMonths: true,
								changeMonth: true,								
								dateFormat: "dd-mm-yy",
								showOn: "both",
								buttonImage:$('#root').val()+"img/calendar.jpg",
								buttonImageOnly: true
	 							});

	function split( val ) {
		return val.split( /,\s*/);
	}
	function extractLast( term ) {
		return split(term).pop();
	}
	$( "#thanhvien" )
	//$( "#thu" )
	// don't navigate away from the field on tab when selecting an item
	.bind( "keydown", function( event ) {
		if ( event.keyCode === $.ui.keyCode.TAB &&
				event.keyCode ===38 && event.keyCode ===40 &&
				$( this ).data( "autocomplete" ).menu.active ) {
			event.preventDefault();
		}
	})
	.autocomplete({
		source: function( request, response ) {
			$.getJSON( "/chitieungays/autocompletenv", {		term: extractLast( request.term )
			}, response );},
		search: function() {
			var term = extractLast( this.value );
			if ( term.length < 1 ) {
				return false;
			}
		},
		focus: function() {
			// prevent value inserted on focus
			$(this).trigger('keydown.autocomplete');
			//return false;
		},
		select: function( event, ui ) {
			var terms = split( this.value );
			// remove the current input
			terms.pop();
			// add the selected item
			terms.push( ui.item.value );
			// add placeholder to get the comma-and-space at the end
			terms.push( "" );
			this.value = terms.join( ", " );
			return false;
		}
	});
	
	function validate()
	{
		if($(".thoigian").val()=='')
		{
			alert('Bạn chưa chọn ngày');
			return false;
		}
		if($("#thanhvien").val()=='')
		{
			alert('Bạn chưa chọn nhân sự');
			return false;
		}if($(".diadiem").val()=='')
		{
			alert('Bạn chưa chọn địa điểm');
			return false;
		}
		return true;
	}
});
	
//-->
</script>
<div class="index">
<?php
$numhh = count($hanghoas); 
echo $form->create('Chitieungay',array('onsubmit'=>'return validate()'));
echo $form->input('root',array('id'=>'root','type'=>'hidden','value'=>$this->webroot));
?>
<table id ='tables'>
<tr>
<th rowspan=2>STT</th>
<th rowspan=2>Đội</th>
<th rowspan=2>Địa Điểm</th>
<th rowspan=2>Ngày</th>
<th rowspan=2>Thành viên tham gia</th>
<th colspan=<?php echo $numhh?>>Chỉ tiêu</th>
<th rowspan=2>Ghi chú</th>
<th rowspan=2>Thao tác</th>
</tr>
<tr>
<?php foreach ($hanghoas as $keyhh =>$valhh):?>
<th><?php echo $valhh ?></th>
<?php endforeach;?>
</tr>
<?php
	$i=0; 
	 foreach ($chitieungays as $chitieungay):
	  $i++;
?>
<tr>
<td><?php echo $i?></td>
<td><?php echo $chitieungay['Kehoachbhld']['tendoi']?></td>
<td><?php echo $chitieungay['Kehoachbhld']['diadiem']?></td>
<td><?php echo date('d-m-Y',strtotime($chitieungay['Kehoachbhld']['thoigian']))?></td>
<td><?php echo $chitieungay['Kehoachbhld']['nhansu']?></td>
<?php foreach ($hanghoas as $keyhh =>$valhh):?>
<td>
<?php
	foreach ($chitieungay['Chitieungay'] as $ctngay)
	{
		if($ctngay['hanghoa_id']==$keyhh)
 			echo $ctngay['soluong'];	
	}
?>
</td>
<?php endforeach;?>
<td><?php echo $chitieungay['Kehoachbhld']['ghichu']?></td>
<td>
<?php echo $html->link('Sửa',array('controller'=>'chitieungays','action'=>'suachitieu',$chitieungay['Kehoachbhld']['id']));?>
<?php echo $html->link('Xóa',array('controller'=>'chitieungays','action'=>'xoakehoach',$chitieungay['Kehoachbhld']['id']));?>
</td>
</tr>
<?php endforeach;?> 
<tr id='them'>
<td><? echo ++$i?></td>
<td><?php echo $form->input('doi',array('label'=>false,'id'=>'doi','type'=>'text' ,'value'=>'Đội '.$i))?></td>
<td><?php //echo $form->input('diadiem',array('label'=>false,'class'=>'diadiem','type'=>'text','value'=>''))?></td>
<td><?php echo $form->input('thoigian',array('label'=>false,'class'=>'thoigian','type'=>'text','value'=>''))?></td>
<td><div id='kenh'><?php echo $form->input('kenh_id',array('id'=>'kenh_id','label'=>'Kênh','multiple'=>"multiple",'options'=>$kenhs))?></div></td>
<!--<td><?php //echo $form->input('thanhvien',array('label'=>false,'id'=>'thanhvien','type'=>'text','value'=>''))?></td>-->
<?php foreach ($hanghoas as $keyhh =>$valhh):?>
<td><input class="soluong" name=data[Chitieungay][hanghoa][<?php echo $keyhh?>]></td>
<?php endforeach;?>
<td><?php echo $form->input('ghichu',array('label'=>false,'id'=>'ghichu','type'=>'textarea','value'=>''))?></td>
<td></td>
</tr>
</table>
<?php echo $form->input('diadiem',array('label'=>'Địa điểm','class'=>'diadiem','type'=>'text','value'=>''))?>

<?php echo $form->input('thanhvien',array('label'=>'Nhân sự','id'=>'thanhvien','type'=>'text','value'=>''))?>
<?php echo $form->end('  Lưu  ');?>
</div>

<?php
/*
$javascript->codeBlock('
jQuery(function($){
$("#textsearch").autocomplete({
 source: "' . $html->url(array('action' => 'autocompletekenh')) . '"
});
  });
', array('inline' => false));*/
?>